package cn.itcast.nio.c4;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

import static cn.itcast.nio.c2.ByteBufferUtil.debugAll;

/**
 *
 */
public class Client {

    public static void main(String[] args) throws IOException {
        SocketChannel sc = null;
        try {
            sc = SocketChannel.open();
            sc.connect(new InetSocketAddress("localhost", 9090));
            sc.write(Charset.defaultCharset().encode("zing"));
            ByteBuffer buffer = ByteBuffer.allocate(16);
            int read = sc.read(buffer);
            if (read > 0) {
                //debugAll(buffer);
                buffer.flip();
                System.out.println("received:"+Charset.defaultCharset().decode(buffer));
            }
            System.out.println("waiting...");
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (sc != null) {
                sc.close();
            }
        }

    }
}
